package com.xu.sms.controller;

import java.util.List;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.xu.sms.R;
import com.xu.sms.adapter.FBOTypeAdapter;
import com.xu.sms.entity.Type;
import com.xu.sms.net.HTTPNet;
import com.xu.sms.utils.LogUtil;
import com.xu.sms.view.MyGridView;

public class FBOTypeFragment extends Fragment {
	
	
	private Context context ;
	private MyGridView fbotype_gl_gl;
	private FBOTypeAdapter fBOAdapter;
	public  static final int  TYPE_PARENT_FESTIVAL_ID = 1;   //类型id
	public  static final int  TYPE_PARENT_BENTSION_ID = 2;   
	public  static final int  TYPE_PARENT_OTHER_ID = 3;   
	private MyOnItemClickListener myOnItemClickListener;
	private String requestUrl = "http://"+HTTPNet.servet_IP_Address+"/sms/admin/type/findTypeByParentId?typeParentid=";
	
	public FBOTypeFragment(){};
	
	//根据传送不同的requestTypeId来获取不同的类型的数据从而达到了代码的复用
	public FBOTypeFragment (int requestTypeId){
		requestUrl +=requestTypeId;
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_fbotype, null);
		context = this.getActivity();
		/*
		 * 【0】.可以尝试模拟数据看看效果
		 * 1.得到ListView
		 * 2.继承baseAdapter设置适配器
		 * 3.在适配器的中设置要显示的数据
		 * 4.给条目设置点击事件（其实能跳转到显示短信列表的页面）
		 */
		fbotype_gl_gl = (MyGridView) view.findViewById(R.id.fbotype_gl_gl);
		if(myOnItemClickListener ==null){
			myOnItemClickListener = new MyOnItemClickListener();
		}
		fbotype_gl_gl.setOnItemClickListener(myOnItemClickListener);
		HTTPNet.requestServerForGet(handler, requestUrl);
		return view;
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			//在父线程更新ui
			if(msg.what == HTTPNet.FIND_TYPE_BY_PARENT_STATE){
				List<Type> typeList = (List<Type>) msg.obj;
				if(fBOAdapter == null){
					fBOAdapter = new FBOTypeAdapter(typeList, context);
				}
				fbotype_gl_gl.setAdapter(fBOAdapter);
			}
		};
	};
	
	class MyOnItemClickListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			/*
			 * 1.拿到你点击的条目的 了Typebean并得到该Typebean的typeId
			 * 2.把该typeId发送到信息展示页面中，信息展示页面拿到该typeId后请求服务器获取信息列表
			 */
			Type type = (Type)parent.getItemAtPosition(position);
			LogUtil.i(this.getClass().getSimpleName(), 85,type.getTypeId()+"——"+type.getTypeName());
			Intent intent = new Intent(context,FBOInfoActivity.class);
			intent.putExtra("typeId",type.getTypeId());
			intent.putExtra("typeName",type.getTypeName());
			startActivity(intent);
		}
	}
}
